package com.rem.photoablum.controller;

import com.rem.photoablum.model.User;
import com.rem.photoablum.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.Session;

/**
 * @author remembrance
 * @version 1.0
 * @date 2022/3/6 21:43
 */
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;

    //登录接口
    //传入username password属性
    @GetMapping("/login")
    public User login(@RequestParam("username")String username,@RequestParam("password")String password,HttpServletRequest request){
        User user = userService.findByUsernameAndPassword(username, password);
        if ( user != null ){
            request.getSession().setAttribute("user",user);
//            System.out.println("login。。。。。。。。");
            return user;
        }
        return null;
    }

    @PutMapping("/register")
    public String register(User user){
        User byUsername = userService.findByUsername(user.getUsername());
        if ( byUsername == null ){
            userService.addUser(user);
            //注册成功后跳转回登录页面，改一下请求地址
            return "ok";
        }else {
            return "error";
        }
    }

}
